package org.finesys.system.controller;

import java.util.Arrays;
import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ConvertUtil;
import org.finesys.common.security.core.annotation.Inner;
import org.finesys.system.api.entity.SysLog;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.service.SysLogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "日志管理模块")
@RestController
@RequiredArgsConstructor
@RequestMapping(ControllerRestApiConfig.LOG)
public class SysLogController {

    private final SysLogService sysLogService;

    /**
     * 新增日志信息表
     *
     * @param sysLog 日志信息表
     * @return R
     */
    @Inner
    @Operation(summary = "新增日志信息", description = "新增日志信息表")
    @PostMapping(value = RestApiConfig.ADD)
    public R<?> save(@RequestBody SysLog sysLog) {
        return R.success(sysLogService.save(sysLog));
    }


    /**
     * 通过id删除日志信息表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id删除日志信息表", description = "通过id删除日志信息表")
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R<?> delete(@RequestBody String[] ids) {
        return R.success(sysLogService.removeBatchByIds(Arrays.asList(ConvertUtil.toLongArray(ids))));
    }


    /**
     * 通过id查询角色信息表
     *
     * @param roleId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping(RestApiConfig.GET)
    public R<?> findById(@PathVariable("id") Long id) {
        return R.success(sysLogService.getById(id));
    }


    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping(RestApiConfig.ALL)
    public R<List<SysLog>> findAll() {
        return R.success(sysLogService.findAll());
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping(RestApiConfig.LIST)
    public R<List<SysLog>> findList(SysLog sysLog) {
        return R.success(sysLogService.findList(sysLog));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(RestApiConfig.PAGE)
    public R<?> getPage(Page<SysLog> page, SysLog sysLog) {
        return R.success(sysLogService.findPage(page, sysLog));
    }

}
